package com.avcdata.national.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.activity.Login_Activity;
import com.avcdata.aoscustommade.activity.Main_Activity;
import com.avcdata.aoscustommade.main.BaseActivity;
import com.avcdata.aoscustommade.utils.AppActivityManager;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.avcdata.national.adapter.PersonalAttentionCategoryAdapter;
import com.avcdata.national.module.UserBrandsInfo;
import com.avcdata.national.module.UserCompetingBrandInfo;
import com.avcdata.national.network.BaseLog;
import com.avcdata.national.network.JsonRunnable;
import com.avcdata.national.network.ThreadPoolUtils;
import com.avcdata.national.utils.TestUtils;
import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 关注品类
 *
 * @author Administrator
 */
public class PersonalAttentionCategoryActivity extends BaseActivity {

    @ViewInject(R.id.left_group)
    private LinearLayout left_group;
    @ViewInject(R.id.right_group)
    private LinearLayout right_group;
    @ViewInject(R.id.title)
    private TextView title;
    @ViewInject(R.id.leftText)
    private TextView leftText;

    @ViewInject(R.id.jiazai_layout)
    private LinearLayout loading;

    private String where = "";

    @ViewInject(R.id.attention_sure_btn)
    private Button attention_sure_btn;

    @ViewInject(R.id.lv)
    private ListView lv;

    private boolean hasConpeting = false;

    List<UserCompetingBrandInfo.ReturnValueBean> list;

    private PersonalAttentionCategoryAdapter adapter;

    @Override
    public void initView() {
        setContentView(R.layout.activity_personal_attention_category);
        AppActivityManager.getAppManager().addActivity(this);
        ViewUtils.inject(this);
        where = getIntent().getStringExtra("where");
    }

    @Override
    public void initInstall() {
        title.setText("选择竞品");
        leftText.setText("返回");
        loading.setVisibility(View.GONE);
//        if(where.equals("login")){
//            left_group.setVisibility(View.GONE);
//        }else{
            left_group.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        adapter = new PersonalAttentionCategoryAdapter(this,list);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(PersonalAttentionCategoryActivity.this,PersonalAddProductActivity.class);
                intent.putExtra("categroy",list.get(i).getCategory());
                intent.putStringArrayListExtra("CompetingBrands",list.get(i).getCompetingBrands());
                intent.putStringArrayListExtra("OtherBrands",list.get(i).getOtherBrands());
                intent.putStringArrayListExtra("Top20Brands",list.get(i).getTop20Brands());
                startActivity(intent);
            }
        });
    }

    @Override
    public void evenControl() {
        left_group.setOnClickListener(this);
        right_group.setOnClickListener(this);
        attention_sure_btn.setOnClickListener(this);
        right_group.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCateSyncInfo();
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.left_group:
                if(where.equals("login")){
//                    Intent intent = new Intent(this, NationalOverViewActivity.class);
//                    intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
//                    startActivity(intent);
                    this.finish();
                }else{
                    this.finish();
                }
                break;
            case R.id.right_group:
                break;
            case R.id.attention_sure_btn:
                if(where.equals("login")){
//                    Intent intent = new Intent(this, NationalOverViewActivity.class);
//                    intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
//                    startActivity(intent);
                    this.finish();
                }else{
                    this.finish();
                }
                break;
            default:
                break;
        }
    }

    private long startTime = 0;//开始点击的时间,全局变量

    @Override
    public void onBackPressed() {
        if(where.equals("login")){
//            Intent intent = new Intent(this, NationalOverViewActivity.class);
//            intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
//            startActivity(intent);
            this.finish();
        }else{
            this.finish();
        }
    }
    /***
     * 获取页面信息
     */
    private void getCateSyncInfo(){
        loading.setVisibility(View.VISIBLE);
        HashMap<String,Object> map =new HashMap<>();
        map.put("selfbrand","A.O.史密斯");
        map.put("lastWeek", new SharedPreferencesUtils(this).getNewWeek());
        ThreadPoolUtils.execute(new JsonRunnable(PersonalAttentionCategoryActivity.this,"HTTPGET", AvcConstants.URLHeaderBase+AvcConstants.URL_CompetingBrand,handler,map,1));
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case -1:
                    loading.setVisibility(View.GONE);
                    break;
                case 1:
                    loading.setVisibility(View.GONE);
                    UserCompetingBrandInfo info  = new Gson().fromJson(msg.obj.toString(),UserCompetingBrandInfo.class);
                    list.clear();
                    StringBuffer sb = new StringBuffer();
                    loading.setVisibility(View.GONE);

                    for(int i=0;i<info.getReturnValue().size();i++){
                        list.add(info.getReturnValue().get(i));
                    }

                    if(info.getReturnValue().get(0).getCompetingBrands().size() == 0 &&
                            info.getReturnValue().get(1).getCompetingBrands().size() == 0 &&
                            info.getReturnValue().get(2).getCompetingBrands().size() == 0 &&
                            info.getReturnValue().get(3).getCompetingBrands().size() == 0 ){
                        hasConpeting = false;
                    }else{
                        hasConpeting = true;
                        Set<String> brands = new HashSet();
                        for(int i=0;i<info.getReturnValue().size();i++){
                            for(int j=0;j<info.getReturnValue().get(i).getCompetingBrands().size();j++){
                                BaseLog.e("循环","品类==>"+info.getReturnValue().get(i).getCategory()+"<品牌==>"+info.getReturnValue().get(i).getCompetingBrands().get(j).trim());
                                brands.add(info.getReturnValue().get(i).getCompetingBrands().get(j).trim());
                            }
                            new SharedPreferencesUtils(PersonalAttentionCategoryActivity.this).setCompetingBrands(info.getReturnValue().get(i).getCategory(),brands);
                            if(info.getReturnValue().get(i).getCompetingBrands().size() !=0){
                                sb.append(info.getReturnValue().get(i).getCategory()+",");
                            }
                            brands.clear();
                        }
                        new SharedPreferencesUtils(PersonalAttentionCategoryActivity.this).setCompetingCategory(sb.deleteCharAt(sb.length()-1).toString());
                    }
                    adapter.notifyDataSetChanged();
//                    for(int i=0;i<info.getReturnValue().size();i++){
//                        list.add(info.getReturnValue().get(i));
//                        Set<String> brands = new HashSet();
//                        for(int j=0;j<info.getReturnValue().get(i).getCompetingBrands().size();j++){
//                            brands.add(info.getReturnValue().get(i).getCompetingBrands().get(j).trim());
//                        }
//                        if(info.getReturnValue().get(i).getCompetingBrands().size() != 0 ){
//                            sb.append(info.getReturnValue().get(i).getCategory()+",");
//                        }
//                        new SharedPreferencesUtils(PersonalAttentionCategoryActivity.this).setCompetingBrands(info.getReturnValue().get(i).getCategory(),brands);
//                    }
//                    new SharedPreferencesUtils(PersonalAttentionCategoryActivity.this).setCompetingCategory(sb.deleteCharAt(sb.length()-1).toString());

                    break;
                case -22:
                    loading.setVisibility(View.GONE);
                    Intent intent = new Intent ();
                    intent.setClass (PersonalAttentionCategoryActivity.this, Login_Activity.class );
                    intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
                    PersonalAttentionCategoryActivity.this.finish ();
                    startActivity ( intent );
                    break;
                default:
                    loading.setVisibility(View.GONE);
                    break;
            }

        }
    };

}
